﻿"use strict";
define(['admin/app', "service/admin/tokenUserService"], function (app) {
    app.controller('tokenUserController', function ($scope, $stateParams, $location, $uibModal, $state, utils, tokenUserService) {
        $scope.query = {
            page: 1, size: 20, account_name: '', id_card: '', name: '', mobile: '', start_time: '', end_time: '', disabled: ''
        };
        $scope.query_ex = {
            page1: 1, size1: 30, page2: 1, size2: 30
        }
        angular.extend($scope.query, $stateParams);
        var methods = {
            textAreaChanged: function (wordCount) {
                $scope.textcount = wordCount - $scope.model.content.length;
                if ($scope.textcount <= 0) {
                    $scope.textcount = 0;
                    $("#textcount").css("color", "red")
                } else {
                    $("#textcount").css("color", "#ccc")
                }
                $("#textcount").text($scope.model.content.length + "/" + $scope.textcount);

                if ($scope.model.content.length > wordCount) {
                    $scope.model.content = $scope.model.content.substr(0, wordCount);
                }
            },
            search: function () {
                tokenUserService.gets($scope.query).success(function (response) {
                    $scope.model = response.data
                });
            },
            detail: function () {
                if ($stateParams.id > 0) {
                    tokenUserService.get({ id: $stateParams.id, queryPageEx: $scope.query_ex }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '查看';
                }
            },
            add: function () {
                $scope.breadcrumb = "新增";
                w5cValidator.setRules({
                    mobile_code: { required: '请填写区号' },
                    mobile: { required: '请填写手机号' },
                    password: { required: '请填写密码' },
                    user_name: { required: '请填写用户名' },
                    card_no: { required: '请填写身份证' },
                    invitation_code: { required: '请填写邀请码' },
                });
            },
            save: function () {
                var modal = utils.confirm({ msg: "确定要保存么", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    var model = $scope.model;
                    $scope.psdisabled = true;
                    tokenUserService.save(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        $scope.psdisabled = false;
                        if (response.result_code == 1000) {
                            methods.goBack();
                        }
                    }).error(function () { $scope.psdisabled = false; });
                });
            },
            search_user_detail_page: function (page1, page2) {
                $scope.query_ex.page1 = page1;
                $scope.query_ex.page2 = page2;
                methods.detail('page', $scope.query_ex);
            },
            updateDisabled: function (merchant_id, isDisabled) {
                var tip = isDisabled == 1 ? "冻结" : "激活";
                var modal = utils.confirm({ msg: "是否需要" + tip + "该会员", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    tokenUserService.updateDisabled(merchant_id, isDisabled).success(function () {
                        methods.search();
                    });
                })
            },
            //updateUserType: function (userId, userType) {
            //    var tip = userType == 5 ? "供应商" : "店铺";
            //    var modal = utils.confirm({ msg: "是否将该会员设置成" + tip, ok: "确定", cancel: "取消" });
            //    modal.result.then(function () {
            //        tokenUserService.updateUserType(userId, userType).success(function () {
            //            methods.search();
            //        });
            //    })
            //},
            goBack: function () {
                history.back();
            },
            search_page: function (page) {
                $scope.query.page = page;
                $location.search('page', $scope.query.page);
            },
            getTokenUserInvitationRanking: function () {
                tokenUserService.getTokenUserInvitationRanking($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            tokenUserInvitationRanking_page: function (page) {
                $scope.query.page = page;
                methods.getTokenUserInvitationRanking('page', $scope.query.page);
            },
            getAuthInfoList: function () {
                tokenUserService.getAuthInfoList($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            authInfo_page: function (page) {
                $scope.query.page = page;
                methods.getAuthInfoList('page', $scope.query.page);
            },
            authInfoDetail: function () {
                if ($stateParams.id > 0) {
                    tokenUserService.getAuthInfo({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                        $scope.model.status_value = response.data.status + '';
                    });
                    $scope.breadcrumb = '详情';
                }
            },
            authInfoAudit: function () {
                var modal = utils.confirm({ msg: "您确定要保存么", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    var oldStatus = $scope.model.status;
                    $scope.model.status = Number($scope.model.status_value);
                    var model = $scope.model;
                    $scope.psdisabled = true;
                    tokenUserService.authInfoAudit(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000) {
                            methods.goBack();
                        }else {
                            $scope.psdisabled = false;
                            $scope.model.status = oldStatus;
                        }
                    }).error(function () { $scope.psdisabled = false; $scope.model.status = oldStatus; });
                });
            },
            resetAuthNum: function (auth_id) {
                var modal = utils.confirm({ msg: "是否需要将该记录的认证次数重置为0", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    tokenUserService.resetAuthNum(auth_id).success(function () {
                        methods.getAuthInfoList();
                    });
                })
            },
            getTokenUserInvitationHis: function () {
                tokenUserService.getTokenUserInvitationHis($stateParams.userId, $stateParams.his_begin_time, $stateParams.his_end_time).success(function (response) {
                    $scope.model = response.data;
                });
            },
            open_increase_horse_power: function (model) {
                $uibModal.open({
                    templateUrl: 'increase_horse_power.html',
                    controller: 'increase_horse_power_ctrl',
                    resolve: {
                        model: function () {
                            return model;
                        }
                    }
                }).result.then(function (model) {
                    methods.search();
                });
            },
            open_modify_pwd: function (model) {
                $uibModal.open({
                    templateUrl: 'modify_pwd.html',
                    controller: 'modify_pwd_ctrl',
                    resolve: {
                        model: function () {
                            return model;
                        }
                    }
                }).result.then(function (model) {
                    methods.search();
                });
            },
            open_update_parent: function (model) {
                $uibModal.open({
                    templateUrl: 'updateParent.html',
                    controller: 'open_update_user_parent_ctrl',
                    resolve: {
                        model: function () {
                            return model;
                        }
                    }
                }).result.then(function (model) {
                    methods.search();
                });
            },
            open_update_user_type: function (model) {
                $uibModal.open({
                    templateUrl: 'open_update_user_type.html',
                    controller: 'open_update_user_type_ctrl',
                    resolve: {
                        model: function () {
                            return model;
                        }
                    }
                }).result.then(function (model) {
                    methods.search();
                });
            },
            turn_red_diamond: function (model) {
                $uibModal.open({
                    templateUrl: 'turnRedDiamond.html',
                    controller: 'turn_red_diamond_ctrl',
                    resolve: {
                        model: function () {
                            return model;
                        }
                    }
                }).result.then(function (model) {
                    methods.search();
                });
            },
            open_img: function (model) {
                $uibModal.open({
                    templateUrl: 'imgshow.html',
                    controller: 'imgshow_Ctrl',
                    resolve: {
                        model: function () {
                            return model;
                        }
                    }
                }).result.then(function (model) {

                });
            }
        }

        angular.extend($scope, methods);
        if ($state.includes('app.tokenUser.detail')) {
            methods.detail();
        } else if ($state.includes('app.tokenUser.tokenUserInvitationRanking')) {
            methods.tokenUserInvitationRanking_page();
        } else if ($state.includes('app.tokenUser.tokenUserInvitationHis')) {
            methods.getTokenUserInvitationHis();
        } else if ($state.includes('app.tokenUser.getAuthInfoList')) {
            methods.getAuthInfoList();
        } else if ($state.includes('app.tokenUser.authInfoDetail')) {
            methods.authInfoDetail();
        } else {
            methods.search();
        }
    });

    app.controller('increase_horse_power_ctrl', function ($scope, $uibModalInstance, utils, tokenUserService, model) {
        $scope.model = model;
        $scope.increaseHorsePower = function () {
            $scope.psdisabled = true;
            tokenUserService.increaseUserHorsePower($scope.model.merchant_id, $scope.model.increaseHorsePower, $scope.model.day).success(function (response) {
                utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                if (response.result_code == 1000)
                    $uibModalInstance.close($scope.model);
                else
                    $scope.psdisabled = false;
            }).error(function () { $scope.psdisabled = false; });
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    });
    app.controller('modify_pwd_ctrl', function ($scope, $uibModalInstance, utils, tokenUserService, model) {
        $scope.model = model;
        $scope.modify_pwd = function () {
            $scope.psdisabled = true;
            tokenUserService.modify_pwd($scope.model.merchant_id, $scope.model.pwd).success(function (response) {
                utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                if (response.result_code == 1000)
                    $uibModalInstance.close($scope.model);
                else
                    $scope.psdisabled = false;
            }).error(function () { $scope.psdisabled = false; });
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    });


    app.controller('open_update_user_type_ctrl', function ($scope, $uibModalInstance, utils, tokenUserService, model) {
        $scope.model = model;
        $scope.updateUserType = function () {
            $scope.psdisabled = true;
            tokenUserService.updateUserType($scope.model.merchant_id, $scope.model.user_type_value).success(function (response) {
                utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                if (response.result_code == 1000)
                    $uibModalInstance.close($scope.model);
                else
                    $scope.psdisabled = false;
            }).error(function () { $scope.psdisabled = false; });
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    });
    app.controller('open_update_user_parent_ctrl', function ($scope, $uibModalInstance, utils, tokenUserService, model) {
        $scope.model = model;
        $scope.updateParent = function () {
            $scope.psdisabled = true;
            tokenUserService.updateUserParent($scope.model.merchant_id, $scope.model.parentMobile).success(function (response) {
                utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                if (response.result_code == 1000)
                    $uibModalInstance.close($scope.model);
                else
                    $scope.psdisabled = false;
            }).error(function () { $scope.psdisabled = false; });
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    });

    app.controller('open_update_user_parent_ctrl', function ($scope, $uibModalInstance, utils, tokenUserService, model) {
        $scope.model = model;
        $scope.updateParent = function () {
            $scope.psdisabled = true;
            tokenUserService.updateUserParent($scope.model.merchant_id, $scope.model.parentMobile).success(function (response) {
                utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                if (response.result_code == 1000)
                    $uibModalInstance.close($scope.model);
                else
                    $scope.psdisabled = false;
            }).error(function () { $scope.psdisabled = false; });
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    });

    app.controller('turn_red_diamond_ctrl', function ($scope, $uibModalInstance, utils, tokenUserService, model) {
        $scope.turnRed = function () {
            $scope.psdisabled = true;
            $scope.model.user_id = model.merchant_id;
            tokenUserService.turnRedDiamond($scope.model).success(function (response) {
                utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                if (response.result_code == 1000)
                    $uibModalInstance.close($scope.model);
                else
                    $scope.psdisabled = false;
            }).error(function () { $scope.psdisabled = false; });
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    });

    app.controller('imgshow_Ctrl', function ($scope, $uibModalInstance, $uibModal, utils, merchantBankCardService, model) {
        $scope.img_url = model;

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    });
});
